package com.meiyou.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.meiyou.mapper.MeiyouDongtaiMapper;
import com.meiyou.mapper.MeiyouUserMapper;
import com.meiyou.mapper.NXDongtaiDianzanMapper;
import com.meiyou.mapper.NXPinglunDianzanMapper;
import com.meiyou.mapper.NewPinglunMapper;
import com.meiyou.mapper.PinglunMapper;
import com.meiyou.mapper.custom.MeiyouDontaiMapperCustom;
import com.meiyou.mapper.custom.PinglunMapperCustom;
import com.meiyou.pojo.DongTai;
import com.meiyou.pojo.DongtaiDianzan;
import com.meiyou.pojo.MeiyouDongtai;
import com.meiyou.pojo.MeiyouUser;
import com.meiyou.pojo.MeiyouUserExample;
import com.meiyou.pojo.MeiyouUserExample.Criteria;
import com.meiyou.pojo.NXDongtaiDianzan;
import com.meiyou.pojo.NXDongtaiDianzanExample;
import com.meiyou.pojo.NXPinglunDianzan;
import com.meiyou.pojo.NXPinglunDianzanExample;
import com.meiyou.pojo.Pinglun;
import com.meiyou.pojo.PinglunExample;
import com.meiyou.pojo.vo.DianZanUser;
import com.meiyou.pojo.vo.PingLunBo;
import com.meiyou.pojo.vo.PinglunUser;
import com.meiyou.service.DongtaiService;

@Service("DongtaiService")
public class DongtaiServiceImpl implements DongtaiService{

	@Autowired
	MeiyouDontaiMapperCustom meiyouDontaiMapperCustom;
	@Autowired
	MeiyouUserMapper meiyouUserMapper;
	@Autowired
	PinglunMapper pinglunMapper;
	@Autowired
	PinglunMapperCustom pinglunMapperCustom;
	@Autowired
	NewPinglunMapper newPinglunMapper;
	@Autowired
	MeiyouDongtaiMapper dongtaiMapper;
	@Autowired
	NXDongtaiDianzanMapper nXDongtaiDianzanMapper;
	@Autowired
	NXPinglunDianzanMapper nXPinglunDianzanMapper;
	
	public void insert(DongTai dongtai) {
		// TODO Auto-generated method stu@b
	    meiyouDontaiMapperCustom.insertDongtai(dongtai); 
	}

	
	public void updatecishu(String dongtaiId) {
		// TODO Auto-generated method stub
		meiyouDontaiMapperCustom.updatecishu(dongtaiId);
	}

	
	public DongTai selectById(String dongtaiId) {
		// TODO Auto-generated method stub
		return meiyouDontaiMapperCustom.selectById(dongtaiId);  
	}

	
	public MeiyouUser selectUserById(String uPhone) {
		// TODO Auto-generated method stub
		MeiyouUserExample example = new MeiyouUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUCountEqualTo(uPhone);
		List<MeiyouUser> list = meiyouUserMapper.selectByExample(example);
		if(list!=null && list.size()!=0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	
	public void adddianzan(String dongtaiId) {
		// TODO Auto-generated method stub
		meiyouDontaiMapperCustom.updlikedianzan(dongtaiId);
	}

	
	public void recevedianzan(String dongtaiId) {
		// TODO Auto-generated method stub
		meiyouDontaiMapperCustom.updunlikequxiaodianzan(dongtaiId); 
		
	}

	
	public int addpinglun(Pinglun pinglun) {
		// TODO Auto-generated method stub
		return pinglunMapper.insert(pinglun);
	}

	
	public void adddianzanpinglun(int id) {
		// TODO Auto-generated method stub
		pinglunMapperCustom.updlikedianzan(id);
	}

	
	public void recevedianzanpinglun(int id) {
		// TODO Auto-generated method stub
		pinglunMapperCustom.updunlikedianzan(id);
	}

	public List<Pinglun> selectAllPinglun(String dongtaiId) {
		// TODO Auto-generated method stub
		
		PinglunExample example = new PinglunExample();
		com.meiyou.pojo.PinglunExample.Criteria criteria = example.createCriteria();
		criteria.andDongtaiIdEqualTo(dongtaiId);
		List<Pinglun> list =pinglunMapper.selectByExample(example);
		
		return list;
	}


	public DongTai selectDongtaiByuccount(String account) {
		// TODO Auto-generated method stub
		
		 return meiyouDontaiMapperCustom.selectDongtaiByuccount(account);
		
	}


	public DongTai selectDongtaiByDongtaiId(String dongtaiId) {
		// TODO Auto-generated method stub
		return meiyouDontaiMapperCustom.selectById(dongtaiId);
	}


	public MeiyouUser selectUserByaccount(String accdount) {
		// TODO Auto-generated method stub
		MeiyouUserExample example = new MeiyouUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUCountEqualTo(accdount);
		List<MeiyouUser> list = meiyouUserMapper.selectByExample(example);
		return list.get(0);
	}


	//根据动态id查询出所有评论（最新）
	public List<PingLunBo> selectPinglunByDongtaiId(String dongtaiId) {
		
		PinglunExample example = new PinglunExample();
		com.meiyou.pojo.PinglunExample.Criteria criteria = example.createCriteria(); 
		criteria.andDongtaiIdEqualTo(dongtaiId);
		List<Pinglun> list = pinglunMapper.selectByExample(example);
		/*List<NewPinglun> list = newPinglunMapper.selectDongtaiPinglunById(dongtaiId);*/
		List<PingLunBo> result = new ArrayList<PingLunBo>();
		if(list!=null){
			for(Pinglun u:list){
				PingLunBo pingLunBo = new PingLunBo();
				//根据id查询用户
			     MeiyouUser user = meiyouUserMapper.selectByPrimaryKey(u.getPlUserId());
			     pingLunBo.setNewPinglun(u);
			     pingLunBo.setMeiyouUser(user);
			     result.add(pingLunBo);
			}
			
		}
		return result;
	}


	public MeiyouDongtai selectDongtaiByDontaiId(String dongtaiId) {
		// TODO Auto-generated method stub
		
		return dongtaiMapper.selectByPrimaryKey(dongtaiId);
	}


	//根据用户名查询他发布的所有动态（最新）
	public List<MeiyouDongtai> selectAllDongtaiByAccount(String account) {
		
		/*MeiyouDongtaiExample dongtaiExample = new MeiyouDongtaiExample();
		com.meiyou.pojo.MeiyouDongtaiExample.Criteria criteria = dongtaiExample.createCriteria();
		criteria.andTUcountEqualTo(account);
		List<MeiyouDongtai> list = dongtaiMapper.selectByExample(dongtaiExample);*/
		List<MeiyouDongtai> list = dongtaiMapper.selectDongtai(account);
		for(MeiyouDongtai m:list){
			 JSONArray array = JSONArray.parseArray(m.gettImg());
		     m.setArray(array);
		 }
		
		return list;
	}


	public List<DongtaiDianzan> panduanshifoudianzan(DongtaiDianzan dongtaiDianzan) {
		
		List<DongtaiDianzan> list = newPinglunMapper.panduanshifoudianzan(dongtaiDianzan);
		return list;
	}


	//<!-- 根据用户Id查询未通知评论  1 表示未读 2 表示已读 -->
		public List<PinglunUser> selectIsNotReadPinglun(int userId) {
			// TODO Auto-generated method stub
			List<PinglunUser> result = new ArrayList<PinglunUser>();
			PinglunExample example = new PinglunExample();
			com.meiyou.pojo.PinglunExample.Criteria criteria = example.createCriteria();
			criteria.andFbUserIdEqualTo(userId);
			criteria.andIsReadEqualTo(1);
			List<Pinglun>  list = pinglunMapper.selectByExample(example);
			for(Pinglun l :list) {
				System.out.println(l);
			}
			if(list!=null) {
				for(Pinglun l :list) {
					//查询用户信息
					MeiyouUser user = meiyouUserMapper.selectByPrimaryKey(l.getPlUserId());
					PinglunUser pu = new PinglunUser();
					pu.setName(user.getuName());
					pu.setPinglunContent(l.getContent());
					pu.setTime(l.getShijian());
					//查询动态内容
					MeiyouDongtai  meiyouDongtai = dongtaiMapper.selectByPrimaryKey(l.getDongtaiId());
					pu.setDongtaiContent(meiyouDongtai.gettContext());
					result.add(pu);
				}
				//修改信息中is_read为2
				newPinglunMapper.updateNotReadToReadForPinglun(userId);
				return result;
			}else {
				return null;
			}
		}

		//<!-- 根据用户Id查询未通知动态点赞   1 表示未读 2 表示已读 -->
		public List<DianZanUser> selectIsNotReadDontaiDianzan(int userId) {
			// TODO Auto-generated method stub
			List<DianZanUser> result = new ArrayList<DianZanUser>();
			NXDongtaiDianzanExample dianzanExample = new NXDongtaiDianzanExample();
			com.meiyou.pojo.NXDongtaiDianzanExample.Criteria criteria = dianzanExample.createCriteria();
			criteria.andFbUserIdEqualTo(userId);
			criteria.andIsReadEqualTo(1);
			List<NXDongtaiDianzan> list = nXDongtaiDianzanMapper.selectByExample(dianzanExample);
			if(list!=null) {
			    for(NXDongtaiDianzan nx:list) {
					DianZanUser dianZanUser = new DianZanUser();
					//查询用户
					MeiyouUser user = meiyouUserMapper.selectByPrimaryKey(nx.getDianzanUserId());
					dianZanUser.setName(user.getuName());
					//查询动态内容
					MeiyouDongtai  meiyouDongtai = dongtaiMapper.selectByPrimaryKey(nx.getDongtaiId());
					dianZanUser.setContent(meiyouDongtai.gettContext());
					result.add(dianZanUser);
			    }
			  //修改信息中is_read为2
			    newPinglunMapper.updateNotReadToReadForDongtaiDianzan(userId);
			    return result;
			}else {
				return null;
			}
		}

		//<!-- 根据用户Id查询未通知评论点赞   1 表示未读 2 表示已读 -->
		public List<DianZanUser> selectIsNotReadPinglunById(int userId) {
			// TODO Auto-generated method stub
			List<DianZanUser> result = new ArrayList<DianZanUser>();
			NXPinglunDianzanExample dianzanExample = new NXPinglunDianzanExample();
			com.meiyou.pojo.NXPinglunDianzanExample.Criteria criteria = dianzanExample.createCriteria();
			criteria.andFbUserIdEqualTo(userId);
			criteria.andIsReadEqualTo(1);
			List<NXPinglunDianzan> list = nXPinglunDianzanMapper.selectByExample(dianzanExample);
			if(list!=null) {
			    for(NXPinglunDianzan nx:list) {
					DianZanUser dianZanUser = new DianZanUser();
					//查询用户
					MeiyouUser user = meiyouUserMapper.selectByPrimaryKey(nx.getDianzanUserId());
					dianZanUser.setName(user.getuName());
					//查询评论内容
					Pinglun  Pinglun = pinglunMapper.selectByPrimaryKey(nx.getPinglunId());
					dianZanUser.setContent(Pinglun.getContent());
					result.add(dianZanUser);
			    }
			  //修改信息中is_read为2
			    newPinglunMapper.updateNotReadToReadForPinglunDianzan(userId);
			    return result;
			}else {
				return null;
			}
		}

	
	
}
